# -*- encoding:utf-8 -*-
# __author__ = 'chenyongbing'
# __poject__ = 'ops_platform'
# __datetime__ = '2022/7/6'
# __name__ = 'acr_helper.py'
from alibabacloud_cr20181201.client import Client as cr20181201Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_cr20181201 import models as cr_20181201_models
import datetime
# Create your code here.


class AcrHelper():
    def __init__(self, access_key_id='', access_key_secret='', endpoint='cr.cn-hangzhou.aliyuncs.com'):
        self.acrclient = self.create_acr_client(access_key_id=access_key_id, access_key_secret=access_key_secret, endpoint=endpoint)

    def create_acr_client(self, access_key_id='', access_key_secret='', endpoint=''):
        config = open_api_models.Config(
            # 您的AccessKey ID,
            access_key_id=access_key_id,
            # 您的AccessKey Secret,
            access_key_secret=access_key_secret
        )
        # 访问的域名
        config.endpoint = endpoint

        acrclient = cr20181201Client(config)
        return acrclient

    def get_repo_id_by_name(self, instance_id='', repo_namespace_name='', repo_name=''):
        get_repository_request = cr_20181201_models.GetRepositoryRequest(
            instance_id=instance_id,
            repo_namespace_name=repo_namespace_name,
            repo_name=repo_name
        )
        # 复制代码运行请自行打印 API 的返回值
        d = self.acrclient.get_repository(get_repository_request)
        return d.to_map().get('body', {}).get('RepoId')

    def get_repo_tags_by_id(self, instance_id='', repo_id='', page_size=3):
        list_repo_tag_request = cr_20181201_models.ListRepoTagRequest(
            instance_id=instance_id,
            repo_id=repo_id,
            page_size=page_size
        )

        # 复制代码运行请自行打印 API 的返回值
        data = self.acrclient.list_repo_tag(list_repo_tag_request)
        return data.to_map().get('body', {}).get('Images', [])

    def get_repo_tags(self, instance_id='', repo_namespace_name='', repo_name='', page_size=3):
        datas = []
        repo_id = self.get_repo_id_by_name(instance_id=instance_id, repo_namespace_name=repo_namespace_name,
                                           repo_name=repo_name)
        if not repo_id:
            return datas
        for image in self.get_repo_tags_by_id(instance_id=instance_id, repo_id=repo_id, page_size=page_size):
            ImageCreate = image.get('ImageCreate')
            d = (datetime.datetime.today() - datetime.timedelta(6)).strftime('%s')
            if ImageCreate < int(d) * 1000: continue
            datas.append({
                'ImageCreate': ImageCreate,
                'Tag': image.get('Tag')
            })
        return datas



if __name__ == '__main__':
    import sys
    access_key_id = sys.argv[1]
    access_key_secret = sys.argv[2]
    instance_id = 'cri-vmoluobd9xm1paej'
    acr = AcrHelper(access_key_id=access_key_id, access_key_secret=access_key_secret)
    data = acr.get_repo_tags(instance_id=instance_id, repo_namespace_name='hncrazydogm-t2', repo_name='cobra-doctor-web', page_size=3)
    print(data)